<?php

namespace App\Logic;

use App\Helpers\FileTool;
use App\Models\Material;
use App\User;
use Illuminate\Http\Request;
use Validator;
use zgldh\QiniuStorage\QiniuStorage;

class MaterialLogic
{
    public static function upLoadImage(Request $request, User $user, $mode = 'doc', $save_db = true)
    {
        $name = $request->input("file_name", "file");
        $validator = Validator::make($request->all(), [
            'file_name' => 'required',
            $name => 'required|file|image|mimes:jpeg,jpg,png,gif',
        ], [
            'name.required' => '请上传图片name',
            $name . '.required' => '请上传图片' . $name,
            'file' => '请上传图片file',
            'image' => '请上传图片image',
            'mimes' => '图片格式错误',
            'size' => '图片大小不符',
        ]);
        if ($validator->fails()) {
            return [false, $validator->errors()->first()];
        }
        $file = $request->file($name);

        $id = hashid_encode($user->id);
        $name = md5($file->getClientOriginalName()) . '.' . $file->getClientOriginalExtension();
        $img = $file->storeAs('images/' . $id, $name);
        //$img = $file->store('images/' . $id);
        if ($save_db) {
            $material = Material::query()->create([
                'user_id' => $user->id,
                'type' => Material::IMAGE,
                'data' => $img,
                'persistent_id' => '',
                'fop_state' => 1,
                'key' => '',
                'mode' => $mode,
                'info' => [
                    'original_name' => $file->getClientOriginalName(),
                    'size' => $file->getClientSize(),
                    'mime_type' => $file->getClientMimeType()
                ]
            ]);
            return [true, $material];
        } else {
            return [true, [
                'path' => $img,
                'url' => FileTool::thumb($img)
            ]];
        }


    }

    public static function uploadAudio(Request $request, User $user)
    {
        $name = $request->input("file_name", "file");
        $validator = Validator::make($request->all(), [
            'file_name' => 'required',
            $name => 'required|file',
        ], [
            'name.required' => '请上传音频KEY',
            $name . '.required' => '请上传音频',
            'file' => '请上传音频',
        ]);
        if ($validator->fails()) {
            return [false, $validator->errors()->first()];
        }
        $file = $request->file($name);
        if (!in_array($file->getClientOriginalExtension(), ['mp3', 'flac', 'm4a'])) {
            return [false, '文件格式有误'];
        }
        $id = hashid_encode($user->id);
        $name = md5($file->getClientOriginalName());// . '.' . $file->getClientOriginalExtension();
        $path = $file->storeAs('audio/' . $id, $name);
        //$path = $file->store('audio/' . $id);
        if ($path) {
            $ss = base64_encode(config('filesystems.disks.qiniu.bucket') . ':job_audio/' . $id . '/' . $name);
            $fop = 'avthumb/mp3/ab/' . config('mp3_ab', '64k') . '/ar/22050/acodec/libmp3lame|saveas/' . $ss;
            $disk = QiniuStorage::disk('qiniu');
            $persistent_fop_id = $disk->persistentFop($path, $fop, 'fufei-read');
            $material = Material::query()->create([
                'user_id' => $user->id,
                'type' => Material::AUDIO,
                'data' => $path,
                'persistent_id' => $persistent_fop_id,
                'fop_state' => 0,
                'key' => '',
                'info' => [
                    'original_name' => $file->getClientOriginalName(),
                    'size' => $file->getClientSize(),
                    'mime_type' => $file->getClientMimeType()
                ]
            ]);
            return [true, $material];
        } else {
            return [false, '上传失败'];
        }
    }

    public static function uploadVideo(Request $request, User $user, $mode = 'doc', $save_db = true)
    {
        $name = $request->input("file_name", "file");
        $validator = Validator::make($request->all(), [
            'file_name' => 'required',
            $name => 'required|file',
        ], [
            'name.required' => '请上传音频KEY',
            $name . '.required' => '请上传音频',
            'file' => '请上传音频',
        ]);
        if ($validator->fails()) {
            return [false, $validator->errors()->first()];
        }
        $file = $request->file($name);
        if (!in_array($file->getClientOriginalExtension(), ['mp4'])) {
            return [false, '文件格式有误'];
        }
        $id = hashid_encode($user->id);
        $name = md5($file->hashName());
        $path = $file->storeAs('video/' . $id, $name);
        if ($path) {
            if ($save_db) {
                $ss = base64_encode(config('filesystems.disks.qiniu_video.bucket') . ':video/' . $id . '/' . $name);
                $fop = 'avthumb/mp4/ab/160k/ar/44100/acodec/libfaac/r/30/vb/2200k/vcodec/libx264/s/1280x720/autoscale/1/stripmeta/0|saveas/' . $ss;
                $disk = QiniuStorage::disk('qiniu');
                $persistent_fop_id = $disk->persistentFop($path, $fop, 'fufei-read');
                $material = Material::query()->create([
                    'user_id' => $user->id,
                    'type' => Material::VIDEO,
                    'data' => $path,
                    'persistent_id' => $persistent_fop_id,
                    'fop_state' => 0,
                    'key' => '',
                    'info' => [
                        'original_name' => $file->getClientOriginalName(),
                        'size' => $file->getClientSize(),
                        'mime_type' => $file->getClientMimeType()
                    ]
                ]);
                return [true, $material];
            } else {
                return [true, [
                    'path' => $path,
                    'url' => FileTool::video($path),
                    'pic' => FileTool::video($path, 'vframe/jpg/offset/1'),
                ]];

            }
        } else {
            return [false, '上传失败'];
        }
    }
}